package com.service.tools;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.service.bean.device.Vehicle;
import com.service.bean.notice.NoticeMessage;
import com.service.bean.notice.NoticeRulePersonBean;
import com.service.bean.notice.NoticeTemplate;
import com.service.bean.signal.GpsBean;
import com.service.bean.signal.GpsSignal;
import com.service.bean.alarm.AlarmInfo;
import com.service.cache.CacheManager;
import com.service.db.impl.DbNotice;
import com.service.other.Logger;

public class NoticeMessageTool {

	public static String getMap(GpsBean gpsBean, NoticeRulePersonBean noticeRulePersonBean, String refText,
			String messagetext) {
		try {
			messagetext = messagetext.replace("[车牌号]", gpsBean.getPlateNO());
			messagetext = messagetext.replace("[项目部]", gpsBean.getProjectname());
			messagetext = messagetext.replace("[车辆类型]", gpsBean.getCategoryName());
			messagetext = messagetext.replace("[GPS号码]", gpsBean.getEquipNum());
//			if (noticeRulePersonBean != null && noticeRulePersonBean.getPersonName() != null) {
//				messagetext = messagetext.replace("[驾驶员]", noticeRulePersonBean.getPersonName());
//			}
			messagetext = messagetext.replace("[驾驶员]", gpsBean.getDriverNames());
		
			messagetext = messagetext.replace("[报警分类]", gpsBean.getAlarmType());
			messagetext = messagetext.replace("[报警名称]", refText);
			messagetext = messagetext.replace("[报警时间]", gpsBean.getGpsTime().toString());

			messagetext = messagetext.replace("[系统时间]", gpsBean.getSystemTime().toString());
			messagetext = messagetext.replace("[车辆地址]", gpsBean.getBaiduLocation());
			if (gpsBean.getAlarmInfo() != null) {
				messagetext = messagetext.replace("[违规详细信息]", gpsBean.getAlarmInfo());
			} else {
				messagetext = messagetext.replace("[违规详细信息]", "");
			}

			messagetext = messagetext.replace("[速度]", gpsBean.getSpeed() + "");
			messagetext = messagetext.replace("[当日里程]", gpsBean.getTodayMile() + "");
			messagetext = messagetext.replace("[脉冲当日里程]", gpsBean.getMileage2() + "");
			if (gpsBean.getAlarmName() != null) {
				messagetext = messagetext.replace("[作业区域]", gpsBean.getAlarmName());
			} else {
				messagetext = messagetext.replace("[作业区域]", "");
			}
			messagetext = messagetext.replace("[油量]", gpsBean.getFuel() + "");
			messagetext = messagetext.replace("[主发转速]", gpsBean.getMotorSpeed() + "");
			messagetext = messagetext.replace("[副发转速]", gpsBean.getAuxiMotorSpeed() + "");
			messagetext = messagetext.replace("[扫盘转速]", gpsBean.getSweepSpeed() + "");
			messagetext = messagetext.replace("[臭味值]", gpsBean.getOdorValue() + "");
			messagetext = messagetext.replace("[可回收垃圾重量]", gpsBean.getRecoWeight() + "");
			messagetext = messagetext.replace("[不可回收垃圾重量]", gpsBean.getUnrecoWeight() + "");
			messagetext = messagetext.replace("[加水状态]", gpsBean.getWaterStatus() + "");
			messagetext = messagetext.replace("[倾倒垃圾]", String.valueOf(gpsBean.getDumpStatus()));
			messagetext = messagetext.replace("[污水状态]", String.valueOf(gpsBean.getSewageStatus()));
			messagetext = messagetext.replace("[扫盘状态]", String.valueOf(gpsBean.getPlateStatus()));
			messagetext = messagetext.replace("[吸嘴状态]", String.valueOf(gpsBean.getNozzleStatus()));
			messagetext = messagetext.replace("[洒水状态]", String.valueOf(gpsBean.getWateringStatus()));
			messagetext = messagetext.replace("[垃圾箱状态]", String.valueOf(gpsBean.getBinStatus()));
			messagetext = messagetext.replace("[填装器状态]", String.valueOf(gpsBean.getStuffStatus()));
			
			messagetext = messagetext.replace("[服务状态]", "正常");
			
		} catch (Exception e) {
			Logger.err(e);
		}
		/**
		 * [服务状态] ServiceStatus
		 **/
		return messagetext;
	}

	public static void checkSendMessag(String SignalID, String GPSNO, String subType,GpsSignal signal,AlarmInfo ai) {

//		new BaiduApiAddress().getAddress(signal);
		
		Vehicle vehicle = CacheManager.instance().getVehicle(signal.gpsNo);
		
		DbNotice dbNotice = new DbNotice(vehicle.company);
		GpsBean gpsBean = new GpsBean(signal,vehicle,ai);	
		gpsBean.setDriverNames(dbNotice.getDriverNames(GPSNO));
		gpsBean.setAlarmCategoryName(dbNotice.getAlertName("AlarmCategory", subType, gpsBean.getCompanyCode()));
		gpsBean.setCategoryName(dbNotice.getCategoryName(signal.gpsNo));		
		
		NoticeRulePersonBean noticeRulePersonBean = null;
		List<NoticeTemplate> list = dbNotice.queryMessageTeamplate(GPSNO, subType);
		
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				NoticeTemplate noticeTemplate = list.get(i);
				List<NoticeRulePersonBean> nrpb = dbNotice.queryNoticeRulePerson(noticeTemplate.getNoticeRuleId());
				if (nrpb != null && nrpb.size() > 0) {
					for (int j = 0; j < nrpb.size(); j++) {
						noticeRulePersonBean = nrpb.get(j);
						NoticeMessage noticeMessage = new NoticeMessage();
						noticeMessage.setMsgTitle(gpsBean.getPlateNO());
						noticeMessage.setNoticeCategory(noticeTemplate.getNoticeCategory());
						noticeMessage.setMsgType("A");
						noticeMessage.setSender("system");
						if (noticeRulePersonBean.getPersonId() != null
								&& noticeRulePersonBean.getPersonId().trim().length() > 0) {
							noticeMessage.setReceiverId(noticeRulePersonBean.getPersonId());
						}
						if (noticeRulePersonBean.getPersonName() != null) {
							noticeMessage.setReceiverName(noticeRulePersonBean.getPersonName());
						}
						if (noticeRulePersonBean.getTelPhone() != null) {
							noticeMessage.setReceiverPhone(noticeRulePersonBean.getTelPhone());
						}

						noticeMessage.setReceiverMail(noticeRulePersonBean.getEmail());
						String messageText = getMap(gpsBean, noticeRulePersonBean, gpsBean.getAlarmCategoryName(),
								noticeTemplate.getTemplateText());
						noticeMessage.setMessageText(messageText);
						noticeMessage.setIsSend(0);
						noticeMessage.setSendTimes(noticeTemplate.getNoticeTimes());
						noticeMessage.setTimeSpan(noticeTemplate.getTimeSpan());
						noticeMessage.setHasSendTimes(0);
						Calendar cal = Calendar.getInstance();
						Date date = new Date();
						cal.setTime(date);
						cal.add(Calendar.MINUTE, noticeTemplate.getTimeSpan());
						noticeMessage.setNextSendTime(new Timestamp(cal.getTime().getTime()));
						noticeMessage.setCompanyCode(dbNotice.getCompanyCode(GPSNO));
						noticeMessage.setAddTime(new Timestamp(cal.getTime().getTime()));
						
						//存入缓存
						CacheManager.instance().getNoticeMessageQueue(vehicle.company).offer(noticeMessage);
					}
				}
			}
		}
	}

}
